home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Mail / pine3.92 / pico / search.c < prev    next >
C/C++ Source or Header  |  1996-03-14  |  11KB  |  410 lines

  1. #if    !defined(lint) && !defined(DOS)
  2. static char rcsid[] = "$Id: search.c,v 4.11 1996/03/15 07:41:11 hubert Exp $";
  3. #endif
  4. /*
  5.  * Program:    Searching routines
  6.  *
  7.  *
  8.  * Michael Seibel
  9.  * Networks and Distributed Computing
  10.  * Computing and Communications
  11.  * University of Washington
  12.  * Administration Builiding, AG-44
  13.  * Seattle, Washington, 98195, USA
  14.  * Internet: mikes@cac.washington.edu
  15.  *
  16.  * Please address all bugs and comments to "pine-bugs@cac.washington.edu"
  17.  *
  18.  *
  19.  * Pine and Pico are registered trademarks of the University of Washington.
  20.  * No commercial use of these trademarks may be made without prior written
  21.  * permission of the University of Washington.
  22.  * 
  23.  * Pine, Pico, and Pilot software and its included text are Copyright
  24.  * 1989-1996 by the University of Washington.
  25.  * 
  26.  * The full text of our legal notices is contained in the file called
  27.  * CPYRIGHT, included with this distribution.
  28.  *
  29.  */
  30. /*
  31.  * The functions in this file implement commands that search in the forward
  32.  * and backward directions. There are no special characters in the search
  33.  * strings. Probably should have a regular expression search, or something
  34.  * like that.
  35.  *
  36.  */
  37.  
  38. #include        <stdio.h>
  39. #include    "osdep.h"
  40. #include    "pico.h"
  41. #include    "estruct.h"
  42. #include        "edef.h"
  43.  
  44. #ifdef    ANSI
  45.     int eq(int, int);
  46.     int expandp(char *, char *, int);
  47. #else
  48.     int eq();
  49.     int expandp();
  50. #endif
  51.  
  52.  
  53. /*
  54.  * Search forward. Get a search string from the user, and search, beginning at
  55.  * ".", for the string. If found, reset the "." to be just after the match
  56.  * string, and [perhaps] repaint the display. Bound to "C-S".
  57.  */
  58.  
  59. /*    string search input parameters    */
  60.  
  61. #define    PTBEG    1    /* leave the point at the begining on search */
  62. #define    PTEND    2    /* leave the point at the end on search */
  63.  
  64.  
  65.  
  66.  
  67. static char *SearchHelpText[] = {
  68. "Help for Search Command",
  69. " ",
  70. "\tEnter the words or characters you would like to search",
  71. "~\tfor, then press ~R~e~t~u~r~n.  The search then takes place.",
  72. "\tWhen the characters or words that you entered ",
  73. "\tare found, the buffer will be redisplayed with the cursor ",
  74. "\tat the beginning of the selected text.",
  75. " ",
  76. "\tThe most recent string for which a search was made is",
  77. "\tdisplayed in the \"Search\" prompt between the square",
  78. "\tbrackets.  This string is the default search prompt.",
  79. "~        Hitting only ~R~e~t~u~r~n or at the prompt will cause the",
  80. "\tsearch to be made with the default value.",
  81. "  ",
  82. "\tThe text search is not case sensitive, and will examine the",
  83. "\tentire message.",
  84. "  ",
  85. "\tShould the search fail, a message will be displayed.",
  86. "  ",
  87. "End of Search Help.",
  88. "  ",
  89. NULL
  90. };
  91.  
  92.  
  93. /*
  94.  * Compare two characters. The "bc" comes from the buffer. It has it's case
  95.  * folded out. The "pc" is from the pattern.
  96.  */
  97. eq(bc, pc)
  98. int bc;
  99. int pc;
  100. {
  101.     if ((curwp->w_bufp->b_mode & MDEXACT) == 0){
  102.     if (bc>='a' && bc<='z')
  103.       bc -= 0x20;
  104.  
  105.     if (pc>='a' && pc<='z')
  106.       pc -= 0x20;
  107.     }
  108.  
  109.     return(bc == pc);
  110. }
  111.  
  112.  
  113. forwsearch(f, n)
  114. {
  115.     register int status;
  116.     int wrapt = FALSE;
  117.     char show[512];
  118.  
  119.     /* resolve the repeat count */
  120.     if (n == 0)
  121.       n = 1;
  122.  
  123.     if (n < 1)            /* search backwards */
  124.       return(0);
  125.  
  126.     /* ask the user for the text of a pattern */
  127.     while(1){
  128.     if ((status = readpattern("Search")) == TRUE){
  129.         break;
  130.     }
  131.     else{
  132.         switch(status){
  133.           case HELPCH:            /* help requested */
  134.         if(Pmaster)
  135.           (*Pmaster->helper)(Pmaster->search_help,
  136.                      "Help for Searching", 1);
  137.         else
  138.           pico_help(SearchHelpText, "Help for Searching", 1);
  139.           case (CTRL|'L'):            /* redraw requested */
  140.         refresh(FALSE, 1);
  141.         update();
  142.         break;
  143.           case  (CTRL|'V'):
  144.         gotoeob(0, 1);
  145.         mlerase();
  146.         curwp->w_flag |= WFMODE;
  147.         return(TRUE);
  148.           case (CTRL|'Y'):
  149.         gotobob(0, 1);
  150.         mlerase();
  151.         curwp->w_flag |= WFMODE;
  152.         return(TRUE);
  153.           default:
  154.         curwp->w_flag |= WFMODE;
  155.         if(status == ABORT)
  156.           emlwrite("Search Cancelled", NULL);
  157.         else
  158.           mlerase();
  159.         return(FALSE);
  160.         }
  161.     }
  162.     }
  163.  
  164.     curwp->w_flag |= WFMODE;
  165.  
  166.     /*
  167.      * This code is kind of dumb.  What I want is successive C-W 's to 
  168.      * move dot to successive occurences of the pattern.  So, if dot is
  169.      * already sitting at the beginning of the pattern, then we'll move
  170.      * forward a char before beginning the search.  We'll let the
  171.      * automatic wrapping handle putting the dot back in the right 
  172.      * place...
  173.      */
  174.     status = 0;        /* using "status" as int temporarily! */
  175.     while(1){
  176.     if(pat[status] == '\0'){
  177.         forwchar(0, 1);
  178.         break;        /* find next occurance! */
  179.     }
  180.  
  181.     if(status + curwp->w_doto >= llength(curwp->w_dotp) ||
  182.        !eq(pat[status],lgetc(curwp->w_dotp, curwp->w_doto + status).c))
  183.       break;        /* do nothing! */
  184.     status++;
  185.     }
  186.  
  187.     /* search for the pattern */
  188.     
  189.     while (n-- > 0) {
  190.     if((status = forscan(&wrapt,&pat[0],PTBEG)) == FALSE)
  191.       break;
  192.     }
  193.  
  194.     /* and complain if not there */
  195.     if (status == FALSE){
  196.     expandp(&pat[0], &show[0], 50);
  197.     emlwrite("\"%s\" not found", show);
  198.     } 
  199.     else if(wrapt == TRUE){
  200.     emlwrite("Search Wrapped");
  201.     }
  202.     else if(status == TRUE)
  203.       emlwrite("");
  204.  
  205.     return(status);
  206. }
  207.  
  208.  
  209. /*
  210.  * Read a pattern. Stash it in the external variable "pat". The "pat" is not
  211.  * updated if the user types in an empty line. If the user typed an empty line,
  212.  * and there is no old pattern, it is an error. Display the old pattern, in the
  213.  * style of Jeff Lomicka. There is some do-it-yourself control expansion.
  214.  * change to using <ESC> to delemit the end-of-pattern to allow <NL>s in
  215.  * the search string.
  216.  */
  217. readpattern(prompt)
  218. char *prompt;
  219. {
  220.     register int s;
  221.     char         tpat[NPAT+20];
  222.     EXTRAKEYS    menu_pat[3];
  223.  
  224.     menu_pat[0].name  = "^Y";
  225.     menu_pat[0].label = "FirstLine";
  226.     menu_pat[0].key   = (CTRL|'Y');
  227.     KS_OSDATASET(&menu_pat[0], KS_NONE);
  228.     menu_pat[1].name  = "^V";
  229.     menu_pat[1].label = "LastLine";
  230.     menu_pat[1].key   = (CTRL|'V');
  231.     KS_OSDATASET(&menu_pat[1], KS_NONE);
  232.     menu_pat[2].name  = NULL;
  233.  
  234.     strcpy(tpat, prompt);    /* copy prompt to output string */
  235.         if(pat[0] != '\0'){
  236.         strcat(tpat, " [");    /* build new prompt string */
  237.         expandp(&pat[0], &tpat[strlen(tpat)], NPAT/2);    /* add old pattern */
  238.         strcat(tpat, "]");
  239.         }
  240.     strcat(tpat, " : ");
  241.  
  242.     s = mlreplyd(tpat, tpat, NPAT, QNORML, menu_pat);
  243.  
  244.     if (s == TRUE)                /* Specified */
  245.         strcpy(pat, tpat);
  246.     else if (s == FALSE && pat[0] != 0)    /* CR, but old one */
  247.         s = TRUE;
  248.  
  249.     return(s);
  250. }
  251.  
  252.  
  253. forscan(wrapt,patrn,leavep)    /*    search forward for a <patrn>    */
  254. int    *wrapt;        /* boolean indicating search wrapped */
  255. char *patrn;        /* string to scan for */
  256. int leavep;        /* place to leave point
  257.                 PTBEG = begining of match
  258.                 PTEND = at end of match        */
  259.  
  260. {
  261.     register LINE *curline;        /* current line during scan */
  262.     register int curoff;        /* position within current line */
  263.     register LINE *lastline;    /* last line position during scan */
  264.     register int lastoff;        /* position within last line */
  265.     register int c;            /* character at current position */
  266.     register LINE *matchline;    /* current line during matching */
  267.     register int matchoff;        /* position in matching line */
  268.     register char *patptr;        /* pointer into pattern */
  269.     register int stopoff;        /* offset to stop search */
  270.     register LINE *stopline;    /* line to stop search */
  271.  
  272.     *wrapt = FALSE;
  273.  
  274.     /*
  275.      * the idea is to set the character to end the search at the 
  276.      * next character in the buffer.  thus, let the search wrap
  277.      * completely around the buffer.
  278.      * 
  279.      * first, test to see if we are at the end of the line, 
  280.      * otherwise start searching on the next character. 
  281.      */
  282.     if(curwp->w_doto == llength(curwp->w_dotp)){
  283.         /*
  284.          * dot is not on end of a line
  285.          * start at 0 offset of the next line
  286.          */
  287.         stopoff = curoff  = 0;
  288.         stopline = curline = lforw(curwp->w_dotp);
  289.         if (curwp->w_dotp == curbp->b_linep)
  290.           *wrapt = TRUE;
  291.     }
  292.     else{
  293.         stopoff = curoff  = curwp->w_doto;
  294.         stopline = curline = curwp->w_dotp;
  295.     }
  296.  
  297.     /* scan each character until we hit the head link record */
  298.  
  299.     /*
  300.      * maybe wrapping is a good idea
  301.      */
  302.     while (curline){
  303.  
  304.         if (curline == curbp->b_linep)
  305.             *wrapt = TRUE;
  306.  
  307.         /* save the current position in case we need to
  308.            restore it on a match            */
  309.  
  310.         lastline = curline;
  311.         lastoff = curoff;
  312.  
  313.         /* get the current character resolving EOLs */
  314.  
  315.         if (curoff == llength(curline)) {    /* if at EOL */
  316.             curline = lforw(curline);    /* skip to next line */
  317.             curoff = 0;
  318.             c = '\n';            /* and return a <NL> */
  319.         } else
  320.             c = lgetc(curline, curoff++).c;    /* get the char */
  321.  
  322.         /* test it against first char in pattern */
  323.         if (eq(c, patrn[0]) != FALSE) {    /* if we find it..*/
  324.             /* setup match pointers */
  325.             matchline = curline;
  326.             matchoff = curoff;
  327.             patptr = &patrn[0];
  328.  
  329.             /* scan through patrn for a match */
  330.             while (*++patptr != 0) {
  331.                 /* advance all the pointers */
  332.                 if (matchoff == llength(matchline)) {
  333.                     /* advance past EOL */
  334.                     matchline = lforw(matchline);
  335.                     matchoff = 0;
  336.                     c = '\n';
  337.                 } else
  338.                     c = lgetc(matchline, matchoff++).c;
  339.  
  340.                 /* and test it against the pattern */
  341.                 if (eq(*patptr, c) == FALSE)
  342.                     goto fail;
  343.             }
  344.  
  345.             /* A SUCCESSFULL MATCH!!! */
  346.             /* reset the global "." pointers */
  347.             if (leavep == PTEND) {    /* at end of string */
  348.                 curwp->w_dotp = matchline;
  349.                 curwp->w_doto = matchoff;
  350.             } else {        /* at begining of string */
  351.                 curwp->w_dotp = lastline;
  352.                 curwp->w_doto = lastoff;
  353.             }
  354.             curwp->w_flag |= WFMOVE; /* flag that we have moved */
  355.             return(TRUE);
  356.  
  357.         }
  358. fail:;            /* continue to search */
  359.         if((curline == stopline) && (curoff == stopoff))
  360.             break;            /* searched everywhere... */
  361.     }
  362.     /* we could not find a match */
  363.  
  364.     return(FALSE);
  365. }
  366.  
  367. /*     expandp:    expand control key sequences for output        */
  368.  
  369. expandp(srcstr, deststr, maxlength)
  370.  
  371. char *srcstr;    /* string to expand */
  372. char *deststr;    /* destination of expanded string */
  373. int maxlength;    /* maximum chars in destination */
  374.  
  375. {
  376.     char c;        /* current char to translate */
  377.  
  378.     /* scan through the string */
  379.     while ((c = *srcstr++) != 0) {
  380.         if (c == '\n') {        /* its an EOL */
  381.             *deststr++ = '<';
  382.             *deststr++ = 'N';
  383.             *deststr++ = 'L';
  384.             *deststr++ = '>';
  385.             maxlength -= 4;
  386.         } else if (c < 0x20 || c == 0x7f) {    /* control character */
  387.             *deststr++ = '^';
  388.             *deststr++ = c ^ 0x40;
  389.             maxlength -= 2;
  390.         } else if (c == '%') {
  391.             *deststr++ = '%';
  392.             *deststr++ = '%';
  393.             maxlength -= 2;
  394.  
  395.         } else {            /* any other character */
  396.             *deststr++ = c;
  397.             maxlength--;
  398.         }
  399.  
  400.         /* check for maxlength */
  401.         if (maxlength < 4) {
  402.             *deststr++ = '$';
  403.             *deststr = '\0';
  404.             return(FALSE);
  405.         }
  406.     }
  407.     *deststr = '\0';
  408.     return(TRUE);
  409. }
  410.